package com.kuqi.crs.workbench.web.controller;

import com.kuqi.crs.settings.domain.User;
import com.kuqi.crs.settings.service.UserService;
import com.kuqi.crs.settings.service.impl.UserServiceImpl;
import com.kuqi.crs.util.DateTimeUtil;
import com.kuqi.crs.util.PrintJson;
import com.kuqi.crs.util.ServiceFactory;
import com.kuqi.crs.util.UUIDUtil;
import com.kuqi.crs.vo.PaginationVO;
import com.kuqi.crs.workbench.domain.Activity;
import com.kuqi.crs.workbench.domain.ActivityRemark;
import com.kuqi.crs.workbench.service.ActivityService;
import com.kuqi.crs.workbench.service.Impl.ActivityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author:kuqi
 * @createTime:2021/9/24 21:57
 * @description:
 */
@WebServlet(name = "ActivityController",
        value = {"/workbench/activity/getUserList.do",
                "/workbench/activity/save.do",
                "/workbench/activity/pageList.do",
                "/workbench/activity/delete.do",
                "/workbench/activity/getUserListAndActivityList.do",
                "/workbench/activity/update.do",
                "/workbench/activity/detail.do",
                "/workbench/activity/getRemarkList.do",
                "/workbench/activity/deleteRemark.do",
                "/workbench/activity/saveRemark.do",
                "/workbench/activity/updateRemark.do"
        })
public class ActivityController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("正在进入activity活动的控制器中");
        String path = request.getServletPath();
        System.out.println(path);
        if ("/workbench/activity/getUserList.do".equals(path)) {
            getUserList(request, response);
        } else if ("/workbench/activity/save.do".equals(path)) {
            save(request, response);
        } else if ("/workbench/activity/pageList.do".equals(path)) {
            pageList(request, response);
        } else if ("/workbench/activity/delete.do".equals(path)) {
            delete(request, response);
        } else if ("/workbench/activity/getUserListAndActivityList.do".equals(path)) {
            getUserListAndActivityList(request, response);
        }else if ("/workbench/activity/update.do".equals(path)) {
            update(request, response);
        }else if ("/workbench/activity/detail.do".equals(path)) {
            detail(request, response);
        }else if ("/workbench/activity/getRemarkList.do".equals(path)) {
            getRemarkListById(request, response);
        }else if ("/workbench/activity/deleteRemark.do".equals(path)) {
            deleteRemark(request, response);
        }else if ("/workbench/activity/saveRemark.do".equals(path)) {
            saveRemark(request, response);
        }else if ("/workbench/activity/updateRemark.do".equals(path)) {
            updateRemark(request, response);
        }
    }

    private void updateRemark(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("修改备注信息操作");
        String id = request.getParameter("id");
        String noteContent = request.getParameter("noteContent");
        String editBy = ((User) request.getSession().getAttribute("user")).getName();
        String editTime = DateTimeUtil.getSysTime();
        String editFlat = "1";

        ActivityRemark ar = new ActivityRemark();
        ar.setId(id);
        ar.setNoteContent(noteContent);
        ar.setEditBy(editBy);
        ar.setEditTime(editTime);
        ar.setEditFlag(editFlat);

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        boolean flag = as.updateRemark(ar);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("success",flag);
        map.put("ar",ar);

        PrintJson.printJsonObj(response,map);
    }

    private void saveRemark(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("添加备注信息操作");
        /**
         * 获取备注信息
         */
        String id = UUIDUtil.getUUID();//创建主键id
        String createTime = DateTimeUtil.getSysTime();//获取当前时间
        String activityId = request.getParameter("activityId");//获取外键id
        String noteContent = request.getParameter("noteContent");//获取备注信息
        String createBy = ((User) request.getSession().getAttribute("user")).getName();
        String editFlag = "0";

        /**
         * 将备注信息封装到remark对象中
         */
        ActivityRemark ar = new ActivityRemark();
        ar.setId(id);
        ar.setCreateTime(createTime);
        ar.setNoteContent(noteContent);
        ar.setActivityId(activityId);
        ar.setCreateBy(createBy);
        ar.setEditBy(editFlag);

        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        boolean flag = as.saveRemark(ar);

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("success",flag);
        map.put("ar",ar);

        PrintJson.printJsonObj(response,map);
    }

    private void deleteRemark(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("正在进入删除备注信息控制器");
        String id = request.getParameter("id");
        System.out.println(id);
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        boolean flag = as.deleteRemark(id);
        PrintJson.printJsonFlag(response,flag);
    }

    private void getRemarkListById(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("正在进入备注信息列表控制器");
        //获取备注信息列表activityId
        String activityId = request.getParameter("activityId");
        //创建service
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        //调用其方法
        List<ActivityRemark> arList = as.getActivityListById(activityId);
        //将返回的参数响应给前端
        PrintJson.printJsonObj(response,arList);
    }

    private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("正在进入详细信息页");
        //获取市场活动的id
        String id = request.getParameter("id");
        System.out.println(id);
        //创建service对象
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        //调用service的方法,获取市场活动对象
        Activity activity = as.detail(id);
        //将市场活动对象存放到请求域中
        request.setAttribute("activity",activity);
        //请求转发
        request.getRequestDispatcher("/workbench/activity/detail.jsp").forward(request,response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("正在进入修改市场活动信息控制器");
        //获取用户输入的数据
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String owner = request.getParameter("owner");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String cost = request.getParameter("cost");
        String description = request.getParameter("description");
        String editTime = DateTimeUtil.getSysTime();
        String editBy = owner;

        //将获取到的数据封装到map中
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("id",id);
        map.put("owner",owner);
        map.put("name",name);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("cost",cost);
        map.put("description",description);
        map.put("editTime",editTime);
        map.put("editBy",editBy);

        //创建service对象
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        //调用其service方法
        boolean flag = as.update(map);

        //将参数传递给前端
        PrintJson.printJsonFlag(response,flag);
    }

    private void getUserListAndActivityList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("正在进入获取用户列表与活动信息控制器");
        //获取前端传递的需要修改的市场活动的信息
        String id = request.getParameter("id");

        //创建ActivityService对象
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());


        //调用service对象方法
        PaginationVO<User> vo = activityService.getUserListAndActivityList(id);

        /*
            返回前端的信息有
            {"userList":[{用户1},{用户2}],"activity":{市场活动信息}}
         */
        PrintJson.printJsonObj(response,vo);

    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("正在进行删除控制");
        //获取前端传递的需要删除的id列表
        String[] ids = request.getParameterValues("id");
        System.out.println(ids);
        //创建service对象
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        //调用service方法
        boolean flag = as.delete(ids);
        //向前端传递json数据
        PrintJson.printJsonFlag(response,flag);
    }

    private void pageList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("正在进入pageList分页查询的控制器中");
        String name = request.getParameter("name");
        String owner = request.getParameter("owner");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String pageNoStr = request.getParameter("pageNo");
        Integer pageNo = Integer.valueOf(pageNoStr);//越过数标签转换成整形
        System.out.println("当前页数===="+pageNo);


        String pageSizeStr = request.getParameter("pageSize");
        //pageSize每页的条数，即按多少条记录分页
        int pageSize = Integer.valueOf(pageSizeStr);//将每页显示多少条记录，转换成整形
        System.out.println("分页显示记录数====="+pageSize);
        //计算pageNo略过条数
        int skipCount = (pageNo-1)*pageSize;

        //将获取到的数据封装到Map中,传给业务层
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("name",name);
        map.put("owner",owner);
        map.put("startDate",startDate);
        map.put("endDate",endDate);
        map.put("pageSize",pageSize);
        map.put("skipCount",skipCount);


        //创建Service对象
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        //调用service方法查询市场活动信息，返回datalist市场活动信息集合与查询到的总数
        //返回的参数存储到vo类中封装，
        PaginationVO<Activity> vo = as.dataList(map);

        //业务层处理返回的数据传给前端
        PrintJson.printJsonObj(response,vo);

    }


    //向数据库添加数据，将用户填写的活动添加
    private void save(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("向数据库存储数据");
        //获取前端传递过来的数据
        String id = UUIDUtil.getUUID();//生成一个32位随机字符串
        String owner = request.getParameter("owner");
        String name = request.getParameter("name");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String cost = request.getParameter("cost");
        String description = request.getParameter("description");
        String createTime = DateTimeUtil.getSysTime();//为当前系统时间
        String createBy = ((User) request.getSession().getAttribute("user")).getName();//当前用户的名称

        //创建一个Activity类用来封装数据
        Activity a = new Activity();
        a.setId(id);
        a.setOwner(owner);
        a.setName(name);
        a.setStartDate(startDate);
        a.setEndDate(endDate);
        a.setCost(cost);
        a.setDescription(description);
        a.setCreateTime(createTime);
        a.setCreateBy(createBy);

        //创建service
        ActivityService as = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        //调用service层的方法
        boolean flag = as.save(a);

        //将标记响应给前端
        PrintJson.printJsonFlag(response, flag);

    }


    //打开模态窗口获取所有的用户列表
    private void getUserList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("获取用户请求信息");

        //动态代理获取ActivityService的实现类对象——》创建Service
        UserService service = (UserService) ServiceFactory.getService(new UserServiceImpl());

        //调用Service的方法
        List<User> userList = service.getUserList();

        //类对象转换成json数组
        PrintJson.printJsonObj(response, userList);
    }

}
